/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.workforce.common.tags;

import java.lang.reflect.Method;
import java.net.URLEncoder;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

/**
 *
 * @author neo
 */
public class FieldImage extends TagSupport {
    private String sid;
    private String src;
    private String uri;
    private String style;
    private String text;
    private String border = "0";
    private String beanName;
    private String property;

    @Override
    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        StringBuffer s = new StringBuffer();
        s.append("<img ");
        if( sid != null ) {
            s.append("id=\"").append(sid).append(s);
        }
        s.append("src=\"");
        if (beanName != null) {
            String val = "";
            Object obj = pageContext.getAttribute(beanName, PageContext.SESSION_SCOPE);
            if (obj != null) {
                Class cls = obj.getClass();
                Method method;
                try {
                    method = cls.getMethod("get" + property.substring(0, 1).toUpperCase() + property.substring(1), new Class[0]);
                    val = (String) method.invoke(obj, new Object[0]);
                    s.append("imageview?file=").append(URLEncoder.encode(val, "UTF-8"));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        } else {
            if (uri != null) {
                try {
                    s.append("/imageview?file=").append(URLEncoder.encode(uri, "UTF-8"));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } else {
                s.append(src);
            }
        }
        s.append("\" ");
        if (border != null) {
            s.append(" border=\"").append(border).append("\"");
        }
        if (text != null) {
            s.append(" alt=\"").append(text).append("\"");
        }
        if (style != null) {
            s.append(" class=\"").append(style).append("\"");
        }
        s.append(">");
        try {
            out.print(s.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SKIP_BODY;
    }

    @Override
    public int doEndTag() throws JspException {
        return super.doEndTag();
    }

    public void setBorder(String border) {
        this.border = border;
    }

    public void setSrc(String src) {
        this.src = src;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    @Override
    public void setId(String sid) {
        this.sid = sid;
    }
}
